package com.edvard.ormlitedemo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.edvard.backup.JinShanBackupActivity;
import com.edvard.backup.QiNiuBackupActivity;
import com.edvard.bean.FirstSubject;
import com.edvard.jsbackup.R;
import com.edvard.util.DatabaseHelper;
import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

/**
 * 数据库操作代码类
 * 
 * @author edvard
 * 
 */
public class MainActivity extends OrmLiteBaseActivity<DatabaseHelper> implements
		OnClickListener {

	private TextView tv;
	private Button btnBackup,btnBackup_2;

	private Dao<FirstSubject, Integer> firstSubjectDao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById();
		setListener();

		isFirstRun();

		// 取得Dao对象
		try {
			firstSubjectDao = getHelper().getFirstSubject();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ormOperate();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (view.getId() == R.id.btn_jump_backup) {
			Intent intent = new Intent(MainActivity.this, JinShanBackupActivity.class);
			MainActivity.this.startActivity(intent);
			MainActivity.this.finish();
		}else if (view.getId() == R.id.btn_jump_qiniu_backup) {
			Intent intent = new Intent(MainActivity.this, QiNiuBackupActivity.class);
			MainActivity.this.startActivity(intent);
			MainActivity.this.finish();
		}
	}

	public void findViewById() {
		tv = (TextView) findViewById(R.id.tv_show);
		btnBackup = (Button) findViewById(R.id.btn_jump_backup);
		btnBackup_2 = (Button) findViewById(R.id.btn_jump_qiniu_backup);
	}

	public void setListener() {
		btnBackup.setOnClickListener(this);
	}

	/**
	 * 数据库的orm操作
	 */
	public void ormOperate() {
		// 往数据库插四条记录
		insertSubjectTest("个人1");
		insertSubjectTest("个人2");
		insertSubjectTest("个人3");
		insertSubjectTest("个人4");
		appendTv("----查询全部一级科目的名称----");
		queryAllTest();
		appendTv("----查询id为3的一级科目名称----");
		queryById(3);
		appendTv("----更改id为3的一级科目名称为'公司'----");
		updateTest();
		appendTv("----删除id为3的一级科目----");
		deleteTest();
	}

	/**
	 * 插入数据
	 * 
	 * @param name
	 */
	public void insertSubjectTest(String name) {
		FirstSubject firstSubject;
		firstSubject = new FirstSubject();
		firstSubject.setSubject_name(name);
		firstSubject.setRate(0.01);
		try {
			firstSubjectDao.create(firstSubject);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 查询当前表的全部数据
	 */
	public void queryAllTest() {
		// 查询全部一级科目
		List<FirstSubject> subjects;
		try {
			subjects = firstSubjectDao.queryForAll();
			for (int i = 0; i < subjects.size(); i++) {
				int id = subjects.get(i).get_id();
				String subject_name = subjects.get(i).getSubject_name();
				Log.d("科目id号为" + id + "的名称为：", subject_name);
				appendTv("科目号为" + id + "的名称为：" + subject_name);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 查询指定id条目的值
	 * 
	 * @param id
	 */
	public void queryById(Integer id) {
		try {
			FirstSubject firstSubject = firstSubjectDao.queryForId(id);
			Log.d("找到id为" + id + "的科目的名称为", firstSubject.getSubject_name());
			appendTv("找到id号为" + id + "的科目名称为" + firstSubject.getSubject_name());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 更新id值为1的数据
	 * 
	 */
	public void updateTest() {
		try {
			FirstSubject firstSubject = firstSubjectDao.queryForId(3);
			Log.d("id为1的值在更改之前名称：", firstSubject.getSubject_name());
			appendTv("id为1的值在更改之前的名称：" + firstSubject.getSubject_name());
			firstSubject.setSubject_name("公司");
			firstSubjectDao.update(firstSubject);
			firstSubject = firstSubjectDao.queryForId(3);
			Log.d("id为1的智在更改之后名称：", firstSubject.getSubject_name());
			appendTv("id为1的智在更改之后的名称" + firstSubject.getSubject_name());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 删除id值为1的数据
	 * 
	 */
	public void deleteTest() {
		FirstSubject firstSubject = new FirstSubject();
		firstSubject.set_id(3);
		try {
			long length = firstSubjectDao.countOf();
			Log.d("当前一级科目的表长为", length + "");
			appendTv("删除之前表的长度为：" + length);
			firstSubjectDao.delete(firstSubject);
			length = firstSubjectDao.countOf();
			Log.d("删除后一级科目的表长为", length + "");
			appendTv("删除之后便的长度为" + length);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 在textview中拼接字符串
	 * 
	 * @param str
	 */
	public void appendTv(String str) {
		tv.append(str + "\n");
	}

	public void isFirstRun(){
		SharedPreferences sharedPreferences = this.getSharedPreferences("share", MODE_PRIVATE);
		boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun", true);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		if (isFirstRun)
		{
			Log.d("debug", "第一次运行");
			editor.putBoolean("isFirstRun", false);
			editor.commit();
		} else
		{
			Intent intent = new Intent(MainActivity.this, QiNiuBackupActivity.class);
			MainActivity.this.startActivity(intent);
		}
	}

}
